<?php
/**
* 购物车模块 工具类
*
* 购物车模块 工具类的注意及说明
* 以下use模块请不要互相伤害
*
* @author    xiecz <xiecz@yunys.com.cn>
* @copyright 2016 QuanZhouYunYiShang Inc.
*/
namespace App\Services\Tool\Car;

use App\Services\Tool\DataBase\SQL;
use Illuminate\Support\Facades\DB;
use Log;
use App\Services\Tool\GeneralTool;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\UserService\UserTool;
use App\Services\ProductsService\ProductTool;


class CarTool{

    function __construct(UserTool $userTool,
                         ProductTempTool $productTempTool,
                         ProductTool $productTool){
        $this->userTool        = $userTool;
        $this->productTempTool = $productTempTool;
        $this->productTool     = $productTool;
    }

    /**
        获取购物车中的总金额
        参数：
            user_id         用户编号
     */
    function get_shop_car_total_fee($input,$type = 1){
        $where  = ['user_id'=>$input['user_id'],
                   'is_select'=>1,
                   'state'=>1];
        if($type == 2) $where = $input;
        $cars   = SQL::get(array('table'=>'shop_cars',
                                 'where'=>$where));
        $total_fee  = 0;
        foreach($cars as $car){
            $price  = $this->get_product_price($car->product_id);

            $result = $this->can_order($car->product_id,$car->colour_name,$car->size_name,$car->quantity,$input['user_id']);
            if(isset($car->is_select) &&
                $car->is_select == 1 &&
                isset($result) &&
                isset($result['state']) &&
                ($result['state'] == 'M00000' || $result['state'] == 'M08025')){
                $total_fee += $price * $car->quantity;
            }
        }
        return GeneralTool::priceFormat($total_fee).'';
    }

    /**
        获取商品运费
     */
    function get_shop_car_postage($input,$type = 1){
        $where  = ['user_id'=>$input['user_id'],
                   'is_select'=>1,
                   'state'=>1];
        if($type == 2) $where = $input;
        $cars   = SQL::get(array('table'=>'shop_cars',
                                 'where'=>$where));
        $total_postage  = 0;
        foreach($cars as $car){
            $result = $this->can_order($car->product_id,$car->colour_name,$car->size_name,$car->quantity,$input['user_id']);

            if(isset($car->is_select) &&
                $car->is_select == 1 &&
                isset($result) &&
                isset($result['state']) &&
                ($result['state'] == 'M00000' || $result['state'] == 'M08025')){
                $postage = $this->get_postage_by_product_temp_id($car->product_temp_id);
                $total_postage += $postage;
            }
        }
        return GeneralTool::priceFormat($total_postage).'';
    }

    /**
     * 获取购物车商品总数 可结算数量 是否全选
     */
    function get_shop_car_quantity($input,$type = 1){
        $cars   = SQL::get(array('table'=>'shop_cars',
                                 'where'=>array('user_id'=>$input['user_id'],
                                                'state'=>1)));
        $good_total_quantity      = 0 ;//商品总数量
        $good_settlement_quantity = 0 ;//商品选中且可买数量
        $is_all_select            = 1 ;//是否全选
        $can_buy_quantity         = 0 ;//可买数量
        foreach($cars as $car){
            $good_total_quantity += 1;$car->quantity;
            $can_buy = $this->can_order($car->product_id,$car->colour_name,$car->size_name,$car->quantity, $input['user_id']);

            if(isset($car->is_select) &&
               ($type == 2 || $car->is_select == 1)&&
               isset($can_buy['state']) &&
               ($can_buy['state'] == 'M00000' || $can_buy['state'] == 'M08025')){
                $good_settlement_quantity += 1;$car->quantity;
            }
            if($can_buy&&$car->is_select==0){
                $is_all_select = 0 ;
            }
            if($can_buy) $can_buy_quantity += 1;$car->quantity;
        }
        if(count($cars) == 0 || $can_buy_quantity == 0) $is_all_select = 0 ;
        return ['good_total_quantity'      => $good_total_quantity,
                'good_settlement_quantity' => $good_settlement_quantity,
                'is_all_select'            => $is_all_select];
    }

    /**
        获取购物车无效商品编号
     */
    function get_shop_car_id_invalid($input){
        $cars = $this->getUserShopCar(['user_id'=>$input]);
        $shop_car_id = [];

        foreach($cars as $car){
            $can_buy = $this->can_order($car->product_id,$car->colour_name,$car->size_name,$car->quantity,$input);
            if(!isset($can_buy['state']) || ($can_buy['state'] != 'M00000' && $can_buy['state'] != 'M08025'))
                $shop_car_id[] = $car->shop_car_id;
        }

        return $shop_car_id;
    }

    /**
        批量删除购物车
     */
    function delete_shop_car_by_id($input, $shop_car_id){
        foreach($shop_car_id as $id){
            if(!is_string($id)) return 'M00004';
            $result = $this->shop_car_clean(['shop_car_id' => $id,
                                             'user_id'     => $input['user_id']]);
        }
        return 'M00000';
    }

    /**
        清除购物车无效商品
     */
    function clean_invaild($input){
        $result = DB::table('shop_cars')
                  ->whereIn('shop_car_id',$input)
                  ->update(['state'=>0,'updated_at'=>time()]);
        return $result;
    }

    /**
        清空购物车
     */
    function shop_car_clean($input){
        $result = SQL::update(['table'=>'shop_cars',
                               'where'=>$input,
                               'update'=>['state'=>0]]);
        return $result;
    }

    /**
        获取购物车内容
     */
    function getShopCarList($input,$type = 1){
        $cars   = SQL::get(array('table'=>'shop_cars',
                                 'where'=>$input['where']));

        //获取购物车商品价格
        $total_fee    = $this->get_shop_car_total_fee(array('user_id'=>$input['user_id']));

        $good_total_quantity      = 0 ;      //商品总数
        $good_settlement_quantity = 0 ;      //可结算商品总数
        $is_all_select            = 1 ;      //是否全选
        $total_postage            = 0 ;
        $vaild                    = [] ;
        $unvaild                  = [] ;
        foreach($cars as $car){
            /**
                作品名称(creates)
                作品图片(creates)
                产品名称(products)
                颜色(shop_cars colours)
                尺码(shop_cars)
                数量(shop_cars)
                价格(function)
                赠品(function)
             */
            $good = $this->get_product_info($car,$type);

            if(isset($good->quantity)) $good_total_quantity += 1;$good->quantity;

            if(isset($good->is_sale) && $good->is_sale){
                $vaild[] = $good;
                if(isset($good->postage)) $total_postage += $good->postage ;
                if(isset($good->quantity) && isset($good->is_select) && $good->is_select) $good_settlement_quantity += 1;$good->quantity;
                if(isset($good->is_select) && !$good->is_select) $is_all_select = 0;
            }else{
                $unvaild[] = $good;
            }

        }

        if(count($vaild) == 0) $is_all_select = 0;

        $total_postage            = GeneralTool::priceFormat($total_postage).'' ;

        return ['good_total_quantity'      => $good_total_quantity,
                'good_settlement_quantity' => $good_settlement_quantity,
                'is_all_select'            => $is_all_select,
                'total_postage'            => $total_postage,
                'goods'                    => ['vaild'   => $vaild,
                                               'unvaild' => $unvaild],
                'total_fee'                => $total_fee];
    }


    /**
        获取商品详情
     */
    function get_product_info($car, $type = 1){

        $result = $this->get_product_detail_for_order($car->product_id,$car->quantity,$car->colour_name,$car->size_name,$type);

        $good = (object)array();
        $good->shop_car_id     = isset($car->shop_car_id) ? $car->shop_car_id . '' : '' ;
        $good->product_id      = isset($car->product_id) ? $car->product_id : '' ;
        $good->product_temp_id = isset($car->product_temp_id) ? $car->product_temp_id : '' ;
        $good->colour_name     = isset($car->colour_name) ? $car->colour_name : '' ;
        $good->size_name       = isset($car->size_name) ? $car->size_name : '' ;
        $good->quantity        = isset($car->quantity) ? $car->quantity : '' ;
        $good->is_select       = isset($car->is_select) ? $car->is_select : '' ;

        $good->user_id            = isset($result['result']->user_id) ? $result['result']->user_id : '';
        $good->brand_id           = isset($result['result']) && isset($result['result']->product_temp) && isset($result['result']->product_temp->brand_id) ?
                                    $result['result']->product_temp->brand_id : '' ;
        $good->original_image     = isset($result['result']) && isset($result['result']->original_image) && isset($result['result']->original_image[0]) ?
                                    [$result['result']->original_image[0]] : [] ;
        $good->thumbnail          = isset($result['result']) && isset($result['result']->thumbnail) && isset($result['result']->thumbnail[0]) ?
                                    [$result['result']->thumbnail[0]] : [] ;
        $good->original_price     = isset($result['result']) && isset($result['result']->original_price) ?
                                    GeneralTool::priceFormat($result['result']->original_price) . '' : '' ;
        $good->preferential_price = isset($result['result']) && isset($result['result']->preferential_price) ?
                                    GeneralTool::priceFormat($result['result']->preferential_price) . '' : '' ;
        $good->has_preferential   = $good->original_price != $good->preferential_price ? 1 : 0;
        $good->stock              = isset($result['result']) && isset($result['result']->stock) ? $result['result']->stock : 0;
        $good->postage            = isset($result['result']) && isset($result['result']->product_temp) && isset($result['result']->product_temp->postage) ?
                                    $result['result']->product_temp->postage : '0';
        $good->is_shelves         = isset($result['result']) && isset($result['result']->is_shelves) ? $result['result']->is_shelves : 0;
        $good->product_temp_name  = isset($result['result']) && isset($result['result']->product_temp) &&
                                    isset($result['result']->product_temp->product_temp_name) ?
                                    $result['result']->product_temp->product_temp_name : '' ;
        $good->brand_name         = isset($result['result']) && isset($result['result']->product_temp) &&
                                    isset($result['result']->product_temp->brand_name) ?
                                    $result['result']->product_temp->brand_name : '' ;
        $good->gift               = isset($result['result']) && isset($result['result']->gift) ? $this->get_product_gifts($result['result']->gift) : [] ;

        $can_buy                  = $this->can_order($good->product_id, $good->colour_name, $good->size_name, $good->quantity, $car->user_id);

        $good->is_sale            = !isset($result['state']) ||
                                    $result['state'] != 'M00000' ||
                                    !isset($can_buy) ||
                                    !isset($can_buy['state']) ||
                                    ($can_buy['state'] != 'M00000' && $can_buy['state'] != 'M08025')?
                                    0 : 1 ;

        if($good->is_sale == 1 && $type == 2){
            $good->overflow_price = isset($result['result']) && isset($result['result']->overflow_price) ?
                                    $result['result']->overflow_price : '' ;
            $good->product_temp   = isset($result['result']) && isset($result['result']->product_temp) ?
                                    $result['result']->product_temp : '' ;
            $good->source_pics    = isset($result['result']) && isset($result['result']->source_pics) ? $result['result']->source_pics : [];
            $good->product_preview_image_ids = isset($result['result']) && isset($result['result']->product_preview_image_ids) ?
                                               $result['result']->product_preview_image_ids : [];
        }

        return $good;
    }

    /**
        立即购买结算页
     */
    function buy_immediately_submit_goods($input, $type = 1){
        $result = [];
        $result['good_total_quantity']      = 1;GeneralTool::priceFormat($input['quantity']);
        $result['good_settlement_quantity'] = 1;GeneralTool::priceFormat($input['quantity']);

        $good   = (object)array();
        $good->product_id      = $input['product_id'];
        $good->product_temp_id = $input['product_temp_id'];
        $good->colour_name     = $input['colour_name'];
        $good->size_name       = $input['size_name'];
        $good->quantity        = $input['quantity'];
        $good->user_id         = $input['user_id'];

        $good = $this->get_product_info($good,$type);

        $result['total_postage'] = $this->get_postage_by_product_temp_id($good->product_temp_id);
        $result['total_postage'] = isset($result['total_postage']) ? GeneralTool::priceFormat($result['total_postage']) . '' : '0';
        $result['goods']         = ['vaild'=>[$good],'unvaild'=>[]];
        $result['total_fee']     = is_numeric($good->preferential_price) ? GeneralTool::priceFormat($good->preferential_price * $input['quantity']).'' : '';
        $result['total_price']   = $result['total_fee'];

        return $result;
    }

    /**
        工具
     */

    //获取购物车商品
    function getUserShopCar($input){
        $cars   = SQL::get(array('table'=>'shop_cars',
                                 'where'=>array('user_id'=>$input['user_id'],
                                                'state'=>1)));
        return $cars;
    }

    /**
        重组赠品
     */
    function get_product_gifts($gifts){
        $gift = [] ;
        foreach($gifts as $gi){
            $ar                 = (object)array();
            $ar->name           = isset($gi->name) ? $gi->name : '' ;
            $ar->gift_id        = isset($gi->id) ? $gi->id.'' : '' ;
            $ar->quantity       = isset($gi->num) ? $gi->num : 0 ;
            $ar->thumbnail      = isset($gi->thumbnail) ? $gi->thumbnail : '' ;
            $ar->original_image = isset($gi->original_image) ? $gi->original_image : '' ;
            $ar->fg_id          = isset($gi->fg_id) ? $gi->fg_id : '' ;
            $ar->gift_type      = isset($gi->gift_type) ? $gi->gift_type : '' ;
            $gift[]             = $ar;
        }
        return $gift;
    }

    /**
        获取购物车可购买产品数量
     */
    function get_shop_car_product_can_buy_num($arr, $user_id){
        $goods = SQL::get(['table' => 'shop_cars',
                           'where' => ['user_id'    => $user_id,
                                       'product_id' => $arr['product_id'],
                                       'state'      => 1]]);
        $num = 0;
        foreach($goods as $good){
            $can_buy = $this->can_order($good->product_id,
                                        $good->colour_name,
                                        $good->size_name,
                                        $good->quantity,
                                        $good->user_id);
            if($can_buy['state'] == 'M00000' || $can_buy['state'] == 'M08025') $num += $good->quantity;
        }
        return $num;
    }

    /**
        other
     */

    /**
        是否为用户
     */
    function isUser($input){
        return $this->userTool->isUser($input);
    }

    /**
        商品是否可购买
     */
    function can_order($product_id, $colour_name, $size_name, $quantity, $user_id){
        return $this->productTool->can_order($product_id, $colour_name, $size_name, $quantity, $user_id);
    }

    /**
        产品编号获取模版编号
     */
    function getTemplateByProductId($input){
        return $this->productTool->getTemplateByProductId($input);
    }

    /**
        获取商品品牌
     */
    function getBrandIdByProductTempId($input){
        return $this->productTempTool->getBrandIdByProductTempId($input);
    }

    /**
        获取商品详情
     */
    function get_product_detail_for_order($product_id, $quantity, $colour_name, $size_name, $type = 1){
        return $this->productTool->get_product_detail_for_order($product_id, $quantity, $colour_name, $size_name, $type);
    }

    /**
        获取用户默认收货地址
     */
    function defaultAddress($input){
        return $this->userTool->defaultAddress($input);
    }

    /**
        商品编号获取商品价格 getCreatePrice
     */
    function get_product_price($input){

        $price = $this->productTool->get_preferential_price_by_product_id($input);
        return $price;
    }
    /**
        获取产品邮费
     */
    function get_postage_by_product_temp_id($input){
        return $this->productTool->get_postage_by_product_temp_id($input);
    }
}

